package models

import (
	"encoding/json"
	"github.com/beego/beego/v2/client/orm"
	"go-cmdb/pkg"
	"time"
)

//Dashboard 看板实例模型
type WebSite struct {
	BModel
	Status        uint8  `orm:"column(status)" json:"status" form:"status"` // 状态(0:被删除啦：1:正常 2:暂停使用)
	Name          string `json:"name" orm:"column(name)" form:"name"`
	UUID          string `json:"uuid" orm:"column(uuid)" form:"uuid"`
	Tmpl          string `json:"tmpl" orm:"column(tmpl)" form:"tmpl"`
	SectionConfig string `json:"section_config" orm:"column(section_config)" form:"section_config"`
}

//MarshalJSON 格式化时间
func (tb WebSite) MarshalJSON() ([]byte, error) {

	type Alias WebSite

	return json.Marshal(struct {
		Alias
		CreatedAt string `json:"created_at"`
		UpdatedAt string `json:"updated_at"`
	}{
		Alias:     (Alias)(tb),
		CreatedAt: tb.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt: tb.UpdatedAt.Format("2006-01-02 15:04:05"),
	})
}

func NewWebSite() *WebSite {
	return &WebSite{}
}

//TableName 表名
func (*WebSite) TableName() string {
	return WebSiteTable
}

//ToJson 表名
func (tb *WebSite) ToJson() string {
	by, _ := json.Marshal(*tb)
	return string(by)
}
func (*WebSite) GetByPage(pageSize, pageIndex uint64) (lst []WebSite, count uint64, err error) {

	db := orm.NewOrm()
	offset := (pageIndex - 1) * pageSize
	q := db.QueryTable(NewWebSite()).Filter("status__gt", 0)
	var c int64
	c, err = q.Count()
	count = uint64(c)
	q.OrderBy("-updated_at").Offset(offset).Limit(pageSize).All(&lst)
	if lst == nil {
		lst = make([]WebSite, 0)
	}
	return

}

type AddWebSiteReq struct {
	Name string `json:"name" form:"name"`
	Tmpl string `json:"tmpl" form:"tmpl"`
}

func (tb *WebSite) Add(req AddWebSiteReq, uid uint64) (err error) {

	now := time.Now()
	db := orm.NewOrm()
	tb.Name = req.Name
	tb.UUID = pkg.UUID()
	tb.Tmpl = req.Tmpl
	tb.Status = 1
	tb.CreatedAt = now
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.CreatedBy = uid
	tb.SectionConfig = ""

	_, err = db.Insert(tb)
	return

}

func (tb *WebSite) Get(id uint64) (m *WebSite, err error) {
	m = NewWebSite()
	db := orm.NewOrm()
	query := db.QueryTable(tb)
	err = query.Filter("id", id).One(m)
	if m == nil {
		m = nil
	}
	return
}

func (tb *WebSite) GetByUUID(uuid string) (m *WebSite, err error) {
	m = NewWebSite()
	db := orm.NewOrm()
	query := db.QueryTable(tb)
	err = query.Filter("uuid", uuid).One(m)
	if m == nil {
		m = nil
	}
	return
}

//type StatusDashReq struct {
//	ID     uint64 `json:"id" form:"id"`
//	Status uint8  `json:"status" form:"status"`
//}

type RemoveWebSiteReq struct {
	ID uint64 `json:"id" form:"id"`
}
type EnableWebSiteReq struct {
	ID     uint64 `json:"id" form:"id"`
	Status uint8  `json:"status" form:"status"`
}

func (tb *WebSite) CStatus(status uint8, id, uid uint64) (err error) {

	now := time.Now()
	tb.ID = id
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.Status = status
	db := orm.NewOrm()
	_, err = db.Update(tb, "status", "updated_at", "updated_by")

	return
}

type UpdateWebSiteReq struct {
	ID   uint64 `json:"id" form:"id"`
	Name string `json:"name" form:"name"`
}

func (tb *WebSite) Update(name string, id, uid uint64) (err error) {

	now := time.Now()
	tb.ID = id
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.Name = name
	db := orm.NewOrm()
	_, err = db.Update(tb, "name", "updated_at", "updated_by")

	return
}

type SaveWebSiteReq struct {
	ID     uint64 `json:"id" form:"id"`
	Config string `json:"config" form:"config"`
}

func (tb *WebSite) SaveWebSiteConfig(config string, id, uid uint64) (err error) {

	now := time.Now()
	tb.ID = id
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.SectionConfig = config
	db := orm.NewOrm()
	_, err = db.Update(tb, "section_config", "updated_at", "updated_by")

	return
}
